﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class PlayerItem
    {
        #region Properties

        [DataMember]
        public List<UriLink> Teams = new List<UriLink>();
        [DataMember]
        public List<UriLink> BattingStats = new List<UriLink>();
        [DataMember]
        public List<UriLink> FieldingStats = new List<UriLink>();
        [DataMember]
        public List<UriLink> PitchingStats = new List<UriLink>();

        [DataMember]
        public UriLink CareerBattingStats { get; set; }
        [DataMember]
        public UriLink CareerFieldingStats { get; set; }
        [DataMember]
        public UriLink CareerPitchingStats { get; set; }


        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string GivenName { get; set; }
        [DataMember]
        public string NickName { get; set; }
        [DataMember]
        public string Notes { get; set; }
        [DataMember]
        public string College { get; set; }
        [DataMember]
        public int? Weight { get; set; }
        [DataMember]
        public double? Height { get; set; }
        [DataMember]
        public string Bats { get; set; }
        [DataMember]
        public string Throws { get; set; }
        [DataMember]
        public DateTime? Debut { get; set; }
        [DataMember]
        public DateTime? FinalGame { get; set; }
        [DataMember]
        public int? BirthYear { get; set; }
        [DataMember]
        public int? BirthMonth { get; set; }
        [DataMember]
        public int? BirthDay { get; set; }
        [DataMember]
        public string BirthCountry { get; set; }
        [DataMember]
        public string BirthState { get; set; }
        [DataMember]
        public string BirthCity { get; set; }
        [DataMember]
        public int? DeathYear { get; set; }
        [DataMember]
        public int? DeathMonth { get; set; }
        [DataMember]
        public int? DeathDay { get; set; }
        [DataMember]
        public string DeathCountry { get; set; }
        [DataMember]
        public string DeathState { get; set; }
        [DataMember]
        public string DeathCity { get; set; }

        #endregion

        public PlayerItem Load(int playerid)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var teams = (
                    from p in context.People
                    from a in context.Appearances
                    from ft in context.FieldedTeams
                    from nn in context.TeamNickNames
                    orderby ft.Yr
                    where a.PersonId == p.PersonId &&
                    ft.FieldedTeamId == a.TeamId &&
                    nn.TeamNickNameId == ft.TeamNickNameId &&
                    p.PersonId == playerid
                    select new {
                        ft.Yr,
                        nn.NickName,
                        ft.FieldedTeamId,
                    }).ToList();

                foreach (var itm in teams)
                {
                    Teams.Add(new UriLink(string.Format(Constants.baseUrl + "team/{0}", itm.FieldedTeamId), string.Format("{0} {1}", itm.Yr, itm.NickName)));
                    BattingStats.Add(new UriLink(string.Format(Constants.baseUrl + "stats/batting/{0}/{1}", playerid, itm.Yr), itm.Yr.ToString()));
                    PitchingStats.Add(new UriLink(string.Format(Constants.baseUrl + "stats/pitching/{0}/{1}", playerid, itm.Yr), itm.Yr.ToString()));
                    FieldingStats.Add(new UriLink(string.Format(Constants.baseUrl + "stats/fielding/{0}/{1}", playerid, itm.Yr), itm.Yr.ToString()));
                }

                this.CareerBattingStats = new UriLink(string.Format(Constants.baseUrl + "stats/batting/{0}", playerid), "Career");
                this.CareerFieldingStats = new UriLink(string.Format(Constants.baseUrl + "stats/fielding/{0}", playerid), "Career");
                this.CareerPitchingStats = new UriLink(string.Format(Constants.baseUrl + "stats/pitching/{0}", playerid), "Career");

                var personal = (from p in context.People
                                where p.PersonId == playerid
                                select p).FirstOrDefault();
                this.Bats = personal.Bats;
                this.BirthCity = personal.BirthCity;
                this.BirthCountry = personal.BirthCountry;
                this.BirthDay = personal.BirthDay;
                this.BirthMonth = personal.BirthMonth;
                this.BirthState = personal.BirthState;
                this.BirthYear = personal.BirthYear;
                this.College = personal.College;
                this.DeathCity = personal.DeathCity;
                this.DeathCountry = personal.DeathCountry;
                this.DeathDay = personal.DeathDay;
                this.DeathMonth = personal.DeathMonth;
                this.DeathState = personal.DeathState;
                this.DeathYear = personal.DeathYear;
                this.Debut = personal.Debut;
                this.FinalGame = personal.FinalGame;
                this.FirstName = personal.FirstName;
                this.GivenName = personal.GivenName;
                this.Height = personal.Height;
                this.Id = personal.PersonId;
                this.LastName = personal.LastName;
                this.NickName = personal.NickName;
                this.Notes = personal.Notes;
                this.Throws = personal.Throws;
                this.Weight = personal.Weight;

                return this;
            }

        }

    }
}